﻿using FRRJIf;
using RJi.FanucInterface3.Common.Internal;
using RJi.FanucInterface3.Models;

namespace RJi.FanucInterface3.DataParser.Internal;

internal class DataNumRegParser : IDataParser
{
    public bool CanHandle(VariableMetadata metadata)
    {
        return metadata.DataType == typeof(DataNumReg);
    }

    public object? Parse(VariableMetadata metadata)
    {
        var numReg = (DataNumReg)metadata.VariableRef;
        var nums = new RbtRegVar[metadata.Count];

        for (var i = 0; i < metadata.Count; i++)
        {
            object value = null!;
            var index = i + metadata.StartIndex;
            if (!numReg.GetValue(index, ref value)) continue;
            nums[i] = new RbtRegVar(index, value);
        }

        if (nums.Length == 0) return null;
        return nums.Length == 1 ? nums[0] : nums;
    }
}